extends Area

onready var ball_script = load("res://balls/TestBall.gd")

var old_ball: Spatial = null

export(String) var new_type: String = ""
export(PackedScene) var new_ball: PackedScene = null

func _ready():
# warning-ignore:return_value_discarded
	connect("body_entered", self, "_body_entered")

func _body_entered(body):
	if not body is ball_script:
		return
	if not body.is_player:
		return
	if body.ball_type == new_type:
		return
	if new_ball == null:
		return
	if not body.transform_data.empty():
		return
	if $AnimationPlayer.is_playing():
		return

	old_ball = body
	body.transform_data = {
		target = $NewCenter.global_transform.origin,
	}
	$AnimationPlayer.play("transform")

func do_transform():
	var p: Spatial = old_ball.get_parent()
	var obj: Spatial = new_ball.instance()
	obj.is_player = true
	p.add_child(obj)
	obj.global_transform = $NewCenter.global_transform

	# some stupid code
	new_type = obj.ball_type

	var particle_effect: Spatial = preload("res://effects/BallParticleEffect.gd").new()
	p.add_child(particle_effect)
	particle_effect.global_transform = $NewCenter.global_transform

	old_ball.queue_free()
